package com.qinghua.blog.controller.admin;

import com.qinghua.blog.po.Blog;
import com.qinghua.blog.po.Tag;
import com.qinghua.blog.po.Type;
import com.qinghua.blog.po.User;
import com.qinghua.blog.service.BlogService;
import com.qinghua.blog.service.TagService;
import com.qinghua.blog.service.TypeService;
import com.qinghua.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("admin")
public class BlogController {

    @Autowired
    BlogService blogService;

    @Autowired
    TypeService typeService;

    @Autowired
    TagService tagService;
    /**
     * 查询所有博客内容并且进行分页
     * @param pageable 封装了分页的基础内容，每页资源数量，排序规则
     * @param blogQuery 进行分页查询的条件
     * @param model
     * @return
     */
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blogQuery, Model model){


        List<Type>list=typeService.findAll();

        model.addAttribute("listType",list);

        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));

        return "admin/blogs";
    }

    /**
     * 此方法根据条件查询所有博客内容并且进行分页
     * @param pageable 封装了分页的基础内容，每页资源数量，排序规则
     * @param blogQuery
     * @param model
     * @return
     */
    @RequestMapping("/blogs/search")
    public String search(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blogQuery, Model model){

        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));

        return "admin/blogs :: blogList";
    }

    /**
     * 跳转到新增博客
     * @param model 把admin/blog_input页面所需的内容初始化
     * @return
     */
    @GetMapping("/blogs/insertBlog")
    public String insBlog(Model model){

        List<Type>list=typeService.findAll();

        List<Tag> tagList =tagService.findAll();

        model.addAttribute("listTag",tagList);

        model.addAttribute("listType",list);

        model.addAttribute("blog",new Blog());

        return "admin/blog_input";
    }

    /**
     * 用于修改博客内容
     * 根据id查询到blog内容并且初始化到修改页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/blogs/{id}/input")
    public String updBlog(Model model,@PathVariable Long id){

        Blog b= blogService.getBlog(id);

        List<Tag> tags = b.getTags();
        StringBuffer tagIds=new StringBuffer();
        for(Tag tag : tags){
            Long tagId = tag.getId();
            tagIds.append(tagId);
            tagIds.append(",");
        }
        tagIds.deleteCharAt(tagIds.length()-1);
        b.setTagIds(tagIds.toString());

        List<Tag> listTag = tagService.findAll();
        List<Type>list=typeService.findAll();

        model.addAttribute("listType",list);

        model.addAttribute("listTag",listTag);

        model.addAttribute("blog",b);

        return "admin/blog_input";
    }

    /**
     * 根据id删除博客内容
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("/blogs/{id}/delete")
    public String delBlog(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("msg","删除成功");
        return "redirect:/admin/blogs";
    }

    /**
     * 新增博客并跳转到列表页面
     * @param blog
     * @param attributes
     * @param session
     * @return
     */
    @PostMapping("/blogs")
    public String postBlog(Blog blog, RedirectAttributes attributes, HttpSession session){

        blog.setUser((User) session.getAttribute("user"));
        Blog b;


        String tagIds = blog.getTagIds();

        String[] split = tagIds.split(",");

        List<Tag> listTag=new ArrayList<>();
        for(String i:split){
            Long tagId=Long.parseLong(i);
            Tag tag=tagService.getTag(tagId);
            listTag.add(tag);
        }
        blog.setTags(listTag);

        blog.setType(typeService.getType(blog.getType().getId()));


        if(blog.getId()!=null){
            //更新blog
            b=blogService.updateBlog(blog.getId(),blog);

        }else{
            //新增blog
            blog.setViews(0);
            b=blogService.saveBlog(blog);
        }

        if(b==null){
            attributes.addFlashAttribute("msg","新增博客失败");
        }else {
            attributes.addFlashAttribute("msg","新增博客成功");
        }
         return "redirect:/admin/blogs";
    }

}
